<template>
  <div class="role-container">
    <!-- 左侧 -->
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: index === activeIndex }"
        @click="changeIndex(index)"
      >
        <div class="role-info">
          <svg-icon :icon-class="index === activeIndex ? 'user-active': 'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <!-- 更多 -->
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- 有些组件是无法注册 js的原生事件  -->
              <el-dropdown-item @click.native="hEdit(item.roleId)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="hDel(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')">添加角色</el-button>
    </div>
    <!-- 右侧 -->
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="first">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <!-- 如果需要通过 key 来获取或设置，则必须设置node-key。 -->
              <el-tree
                ref="tree"
                :data="item.children"
                :props="defaultProps"
                :default-expand-all="true"
                :show-checkbox="true"
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="second">
          <div class="user-wrapper">
            <el-table
              v-loading="loading"
              :data="roleUserList"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>
</template>

<script>
import { delRoleUserAPI, getSysRoleAPI, getSysRoleDetailAPI, getSysRoleTreeAPI, getUserRoleListAPI } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      loading: true,
      roleUserList: [], // 保存成员
      activeName: 'first',
      roleList: [], // 存储所有的角色
      activeIndex: 0,
      treeList: [], // 权限树形列表
      defaultProps: {
        label: 'title',
        children: 'children',
        disabled: () => true
      },
      perms: [] // 角色对应的权限点
    }
  },
  // 被async 修饰后的函数 它也是异步
  async created() {
    await this.getSysRole()
    await this.getSysRoleTree()
    // 手动的调用 changeIndex方法 让它帮我去请求第一个角色的数据
    this.changeIndex(0)
  },
  methods: {
    hEdit(id) {
      this.$router.push(`/roleAdd?id=${id}`)
    },
    hDel(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await delRoleUserAPI(id)
          this.$message.success('删除角色成功')
          this.getSysRole()
        } catch (error) {
          if (error.response) {
            this.$message.error(error.response.data.msg)
          }
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 获取角色对应的成员
    async getUserRoleLis(id) {
      try {
        const res = await getUserRoleListAPI(id)
        // console.log(res)
        this.roleUserList = res.data.rows
        this.loading = false
      } catch (error) {
        if (error.response) {
          this.$message.error(error.response.data.msg)
        }
      }
    },
    // 获取所有的权限点
    async getSysRoleTree() {
      try {
        const res = await getSysRoleTreeAPI()
        // console.log(res)
        this.treeList = res.data
        // console.log(this.treeList)
        this.setDisabled(this.treeList)
      } catch (error) {
        if (error.response) {
          this.$message.error(error.response.data.msg)
        }
      }
    },
    // 获取角色对应的权限点
    async getSysRoleDetail(id) {
      try {
        const res = await getSysRoleDetailAPI(id)
        this.perms = res.data.perms
        // 处理 节点选中 this.$refs.tree 组件
        // console.log(this.$refs.tree)
        this.$refs.tree.forEach((item, index) => {
          // console.log(item)
          item.setCheckedKeys(this.perms[index])
        })
      } catch (error) {
        if (error.response) {
          this.$message.error(error.response.data.msg)
        }
      }
    },
    // 切换索引号
    changeIndex(index) {
      this.activeIndex = index
      // 获取对象角色的 roleId属性值
      const id = this.roleList[index].roleId
      // console.log(id)
      // 切换索引号 发送请求获取对应的权限点
      this.getSysRoleDetail(id)
      // 获取角色对应的成员
      this.getUserRoleLis(id)
    },
    // 获取角色
    async getSysRole() {
      try {
        const res = await getSysRoleAPI()
        // console.log(res)
        this.roleList = res.data
      } catch (error) {
        if (error.response) {
          this.$message.error(error.response.data.msg)
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
